package com.risetechnology.adapter;

import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.risetechnology.common.Shared;
import com.risetechnology.data.ShopIngredient;
import com.risetechnology.krystalkitchen1.ActivityShoppingDetails;
import com.risetechnology.krystalkitchen1.R;

public class ShoppingListDetailsAdapter extends ArrayAdapter<ShopIngredient>
		implements OnClickListener, OnLongClickListener {

	private Context context;
	private int layoutId;
	private List<ShopIngredient> data = null;
	private Vector<Integer> checked;

	public ShoppingListDetailsAdapter(Context context, int layoutId,
			List<ShopIngredient> data) {
		super(context, layoutId, data);
		this.context = context;
		this.layoutId = layoutId;
		this.data = data;

		if(Shared.shopItems.containsKey(ActivityShoppingDetails.shoppingId)){
			checked = Shared.shopItems.get(ActivityShoppingDetails.shoppingId);
		}else{
			checked = new Vector<Integer>();
		}
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		final ShopIngredient sIng = data.get(position);

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(layoutId, parent, false);
		}

		TextView textviewIngredient = (TextView) convertView
					.findViewById(R.id.tv_ingredient);
		LinearLayout layoutDelete = (LinearLayout) convertView
					.findViewById(R.id.ll_delete);
		ImageView imageviewCheckbox = (ImageView) convertView
					.findViewById(R.id.iv_checkbox);


		textviewIngredient.setText(sIng.toString());
		textviewIngredient.setSelected(true);
		
		layoutDelete.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

			}
		});

		final LinearLayout tempView = (LinearLayout) convertView;
		imageviewCheckbox.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				if (checked.contains(sIng.getID())) {
					checked.remove((Object) sIng.getID());
				} else {
					checked.add(sIng.getID());
				}

				notifyDataSetChanged();
			}
		});

		if (checked.contains(sIng.getID())) {
			imageviewCheckbox.setImageResource(R.drawable.ic_tick);
			tempView.setBackgroundColor(context.getResources().getColor(
					R.color.bg_calendar_header));
		} else {
			imageviewCheckbox.setImageResource(R.drawable.ic_untick);
			tempView.setBackgroundColor(context.getResources().getColor(
					R.color.bg_calendar_item_active));
		}

		layoutDelete.setTag(sIng.getID());
		layoutDelete.setOnClickListener(this);

		convertView.setTag(sIng);
		convertView.setOnLongClickListener(this);


		return convertView;
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onLongClick(View arg0) {
		
		return true;
	}

	/**
	 * @return the checked
	 */
	public Vector<Integer> getChecked() {
		return checked;
	}

	/**
	 * @param checked the checked to set
	 */
	public void setChecked(Vector<Integer> checked) {
		this.checked = checked;
	}

	
	
}
